<template>
  <div>
    <n-spin :show="show" description="请稍候...">
      <n-form :label-width="100" :model="formValue" :rules="rules" ref="formRef">
        <n-divider title-placement="left">核销通知消息</n-divider>
        <n-form-item label="消息模板 Key" path="mpAfterVerifiedKey">
          <n-input v-model:value="formValue.mpAfterVerifiedKey" placeholder="" />
          <template #feedback>请填写小程序消息模板 key</template>
        </n-form-item>

        <n-form-item label="跳转页面地址" path="mpAfterVerifiedPage">
          <n-input v-model:value="formValue.mpAfterVerifiedPage" placeholder="" clearable />
          <template #feedback>请填写小程序消息跳转页面地址</template>
        </n-form-item>

        <n-divider title-placement="left">挽留消息</n-divider>
        <n-form-item label="挽留消息 Key" path="mpRetainKey">
          <n-input v-model:value="formValue.mpRetainKey" placeholder="" />
          <template #feedback>请填写消息模板 Key</template>
        </n-form-item>

        <n-form-item label="跳转页面地址" path="mpRetainPage">
          <n-input v-model:value="formValue.mpRetainPage" placeholder="" clearable />
          <template #feedback>请填写小程序消息跳转页面地址</template>
        </n-form-item>

        <n-form-item label="消息内容" path="mpRetainContent">
          <n-input v-model:value="formValue.mpRetainContent" placeholder="" clearable />
          <template #feedback>请填写消息内容</template>
        </n-form-item>

        <n-form-item label="延迟时长/秒" path="mpRetainSec">
          <n-input-number v-model:value="formValue.mpRetainSec" placeholder="" clearable />
          <template #feedback>延迟多久发送消息/秒</template>
        </n-form-item>

        <!--        <n-divider title-placement="left">小程序</n-divider>-->

        <div>
          <n-space>
            <n-button type="primary" @click="formSubmit">保存更新</n-button>
          </n-space>
        </div>
      </n-form>
    </n-spin>
  </div>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { useMessage } from 'naive-ui';
  import { getConfig, updateConfig } from '@/api/sys/config';

  const group = ref('mp_sub');
  const show = ref(false);
  const rules = {
    mpRetainContent: {
      max: 20,
      message: '消息内容不能超过20个字符',
    },
  };
  const formRef: any = ref(null);
  const message = useMessage();
  const formValue = ref({
    mpAfterVerifiedKey: '',
    mpAfterVerifiedPage: '',
    mpRetainKey: '',
    mpRetainPage: '',
    mpRetainContent: '',
    mpRetainSec: 0,
  });

  function formSubmit() {
    formRef.value.validate((errors) => {
      if (!errors) {
        updateConfig({ group: group.value, list: formValue.value }).then((_res) => {
          message.success('更新成功');
          load();
        });
      } else {
        message.error('验证失败，请填写完整信息');
      }
    });
  }

  onMounted(() => {
    load();
  });

  async function load() {
    show.value = true;
    new Promise((_resolve, _reject) => {
      getConfig({ group: group.value })
        .then((res) => {
          formValue.value = res.list;
        })
        .finally(() => {
          show.value = false;
        });
    });
  }
</script>
